import { login } from '@/api/login'
import { getCompanyStyle, setCompanyStyle } from '@/api/company-style'
import { getUserByCompany } from '@/api/user'

import crypto from '../../crypto'

const types = {
  termsOfUse: 'updateTermsOfUse',
  privacyPolicy: 'updatePrivacyPolicy'
}
const user = {
  state: {
    userInfo: null,
    userByCompany: null,
    userId: null,
    /**
     * @important 修改`companyInfo`时,
     * @important 记得同时修改`src\components\common\Header.vue`
     * @important 下的[methods](logout)
     */
    companyInfo: {
      setStyle: 'white'
    },
    isTermsOfUse: false,
    isPrivacyPolicy: false
  },

  mutations: {
    /***
     * 更新主题的背景颜色
     * @param state
     * @param newStyle
     */
    changeStyle(state, newStyle) {
      if (!!newStyle) {
        state.companyInfo.setStyle = newStyle
        localStorage.setItem("companyInfo", crypto.set(JSON.stringify(state.companyInfo)));
      }
    },
    changeUserInfo(state, newUser) {
      if (newUser) {
        localStorage.setItem('userInfo', crypto.set(JSON.stringify(newUser)));
        state.userInfo = newUser
      }

    },
    changeUserId(state, newUserId) {
      if (newUserId) {
        localStorage.setItem("userId", JSON.stringify(newUserId));
        if (newUserId && newUserId != 'null') {
          state.userId = newUserId
        }
      }
    },
    //修复主题信息
    changeCompanyInfo(state, newCompanyInfo) {
      if (newCompanyInfo && newCompanyInfo != 'null') {
        localStorage.setItem("companyInfo", crypto.set(JSON.stringify(newCompanyInfo)));
        if (!!newCompanyInfo.id || !!newCompanyInfo.companyId) {
          state.companyInfo = newCompanyInfo
        }
      }
    },
    changeUserByCompany(state, newUserByCompany) {
      if (newUserByCompany) {
        localStorage.setItem("userByCompany", JSON.stringify(newUserByCompany));
        if (newUserByCompany && newUserByCompany != 'null') {
          state.userByCompany = newUserByCompany
        }
      }
    },
    [types.termsOfUse](state, is) {
      state.isTermsOfUse = !!is
    },
    [types.privacyPolicy](state, is) {
      state.isPrivacyPolicy = !!is
    }
  },
  // 1.this.$store.dispatch("GetCompanyStyle").then
  // 2. ...mapActions(["Login"]) const { Login } = this  Login().then
  actions: {
    //修复主题颜色
    setStyleColor({ commit }, color) {
      commit("changeStyle", color)
    },

    // 登录
    Login({ commit }, parameter) {
      return new Promise((resolve, reject) => {
        login(parameter).then(response => {
          const result = response.data;
          if (result.code == 200) {
            sessionStorage.setItem("Authorization", result.data.token);
            commit("changeUserInfo", result.data.userInfo);
            commit("changeUserId", result.data.userInfo.id);
          }
          resolve(response.data)
        }).catch(error => {
          reject(error)
        })
      })
    },

    GetCompanyStyle({ commit }) {
      return new Promise((resolve, reject) => {
        getCompanyStyle().then(response => {
          const { code, data } = response.data
          if (code == 200) commit("changeCompanyInfo", data);
          resolve(response.data || { data: {} })
        }).catch(error => {
          reject(error)
        })
      })
    },

    SetCompanyStyle({ commit }, parameter) {
      return new Promise((resolve, reject) => {
        setCompanyStyle(parameter).then(response => {
          const { code, data } = response.data || {};
          if (code == '200') commit("changeCompanyInfo", data);
          resolve(response.data)
        }).catch(error => {
          reject(error)
        })
      })
    },

    GetUserByCompany({ commit }, userByCompanyInfo) {
      return new Promise((resolve, reject) => {
        getUserByCompany(userByCompanyInfo).then(response => {
          const result = response.data.data;
          commit("changeUserByCompany", result)
          resolve(response.data)
        }).catch(error => {
          reject(error)
        })
      })
    },

    editTermsOfUse({ commit }, is) {
      commit(types.termsOfUse, is)
    },

    editPrivacyPolicy({ commit }, is) {
      commit(types.privacyPolicy, is)
    }

  }
}


export default user
